package com.example.androidlearning.sy10.sy1shared;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidlearning.R;

public class Shared extends AppCompatActivity {
    private EditText nameText;
    private EditText ageText;
    private TextView result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shared_sy1001);

        nameText = findViewById(R.id.name);
        ageText = findViewById(R.id.age);
        result = findViewById(R.id.showText);

        Button b1 = findViewById(R.id.button);
        Button b2 = findViewById(R.id.showButton);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a=nameText.getText().toString();
                String b=ageText.getText().toString();
                SharedPreferences.Editor ed=getSharedPreferences("data",MODE_PRIVATE).edit();
                ed.putString("name",a);
                ed.putString("age",b);
                ed.apply();
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a;
                String b;
                SharedPreferences get=getSharedPreferences("data",MODE_PRIVATE);
                a=get.getString("name","");
                b=get.getString("age","");
                nameText.setText(a);
                ageText.setText(b);
                result.setText("姓名："+a+" "+"年龄："+b);
            }
        });
    }
}

